<!--templates-->
<div class="animated fadeIn" >
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <i class="fa fa-align-justify"></i>模块列表
                </div>
                <div class="card-body">
                    <div class=" form-horizontal">
                        <div class="row" >
                            <a shiro:hasPermission="sys:module:add"  class="opreate-btn layui-btn layui-btn-sm" href="#" data-type="add" id="addbtn"><i class="icon-plus"></i>新增</a>
                            <a shiro:hasPermission="sys:module:edit" class="opreate-btn layui-btn layui-btn-sm" href="#" data-type="edit" id="editbtn"><i class="icon-plus"></i>修改</a>
                            <a shiro:hasPermission="sys:module:delete" class="opreate-btn layui-btn layui-btn-sm layui-btn-danger"  href="#" data-type="delete" id="deletebtn"><i class="icon-close"></i>删除</a>
                        </div>
                        <table class="layui-table table-responsive-sm" id="table" lay-filter="table"></table>
                    </div>
                </div>
            </div>
            <!--/.row-->
        </div>
    </div>
</div>

<!--js-->
<script th:inline="javascript">

    var dic_ifShow={"0":"否","1":"是"	};
    var ids=[];
    var deleteFlag=true;
    var moduleName="";

    layui.use('table', function(){
        var table = layui.table,$ = layui.jquery;
        var opt={
            elem: '#table'
            ,url:'/api/sys/module/getModuleForPaging'
            ,height: 410
            ,page: false //开启分页,
            ,cols: [[
                {type:'checkbox'}
                ,{field:'name', width:150, title: '模块名称'}
                ,{field:'indexPage', width:150, title: '模块首页'}
                ,{field:'ifShow', width:100, title: '是否展示', templet: '#ifShowTpl'}
                ,{field:'sort', width:100, title: '排列序号'}
                ,{field:'description', width:250, title: '模块描述'}
            ]],
            response: {
                countName: 'total', //数据总数的字段名称，默认：count
                dataName: 'rows' //数据列表的字段名称，默认：data
            }
        };

        table.render(opt);

        $('#deletebtn').on('click', function(){
            var checkStatus = table.checkStatus('table')
                    ,data = checkStatus.data;
            moduleName="";
            if(data.length==0){
                layer.alert("请选择一条记录!", {icon: 0});
            }else {
                getDeleteIds().then(deleteIds())
            }
        });

        function getDeleteIds() {
            return new Promise (function(resolve, reject) {
                var checkStatus = table.checkStatus('table')
                        ,data = checkStatus.data;
                if(data.length==0){

                }else {
                    $.each(data,function(key,value){
                        getJSON("/api/sys/menu/getMenuForPaging?moduleId="+data[key].id)
                                .catch( error => {

                                })
                                .then( value => {
                                    var rows=JSON.parse(value);
                                    if(rows.length>0){
                                        moduleName=moduleName+" "+data[key].name;
                                        deleteFlag=false;
                                    }else{
                                        ids.push(data[key].id);
                                    }
                                })
                    })
                }
            });
        }
        function deleteIds(){
            layer.alert('请确认是否删除该模块!', {
                icon: 3, btn: ['确认'], yes: function (index) {
                    layer.close(index);
                    if(deleteFlag) {
                        postJSON("/api/sys/module/delete", ids.toString())
                                .catch(error => {

                                })
                                .then(value => {
                                    var result=JSON.parse(value);
                                    if (result == true) {
                                        layer.alert('该模块删除成功!', {
                                            icon: 6, btn: ['确认'], yes: function (index) {
                                                layer.close(index);
                                                var tableIns=table.render(opt);
                                                tableIns.reload();
                                            }
                                        });
                                    } else {
                                        layer.alert("该模块删除失败!", {icon: 2});
                                    }
                                });
                    } else {
                        layer.alert(moduleName+'存在菜单,不能删除!', {icon: 2});
                    }
                }
            })
        }

        $('#addbtn').on('click', function(){
            setUpUrl("sys/module/getListPage@sys/module/addPage");
        });

        $('#editbtn').on('click', function(){
            var checkStatus = table.checkStatus('table')
                    ,data = checkStatus.data;
            if(data.length!=1){
                layer.alert("请选择一条记录!", {icon: 0});
            }else {
                setUpUrl("sys/module/getListPage@sys/module/editPage?moduleId=" + data[0].id);
            }
        });
    });

</script>
<!--模板 js start-->
<script type="text/html" id="ifShowTpl">
    {{#   for (var key in dic_ifShow) { }}
    {{#      if (key == d.ifShow) { }}
    {{             dic_ifShow[key]}}
    {{#      } }}
    {{#  } }}
</script>
<!--模板 end-->

<!--css-->
<style type="text/css">
    .opreate-btn
    {
        width: 60px;
        margin:0 0 10px 15px;
        text-decoration : none;
    }
</style>